package com.neusoft.lohas.common.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import com.neusoft.lohas.common.utils.ActionUtil;

/**
 * 字符串工具
 * @author Administrator
 *
 */
public class StringUtil {

	/**
	 * 如果参数的第一个字符是小写的 ，那就转化为大写的
	 * @param input 长度大于1的String ,大于1是我们的一个约定
	 * @return
	 */
	public static String firstChar2Upcase(String input)
			throws NullPointerException
	{
		if (input == null || input.length() < 1)
			throw new NullPointerException();
		char[] chars = input.toCharArray();
		if (chars[0] >= 'a' && chars[0] <= 'z')
			return (char) (chars[0] + ('A' - 'a')) + input.substring(1);
		else {
			return input;
		}
	}

	/**
	 * 计算字符长度，如果是null就是-1
	 * @param input
	 * @return
	 */
	public static int getStringLength(String input)
	{
		if (input == null)
			return -1;
		return input.length();
	}

	public static int getLength(String input)
	{
		return getStringLength(input);
	}

	public static String subString(String input, int maxlength) {
		if (input == null)
			return null;
		int length = input.length() > maxlength ? maxlength : input.length();
		return input.substring(0, length);
	}

	/**
	 * 如果第一个字符为大写则转换为小写
	 * @param input
	 * @return
	 * @throws NullPointerException
	 */
	public static String firstChar2Downcase(String input)
			throws NullPointerException
	{
		if (input == null || input.length() < 1)
			throw new NullPointerException();
		char[] chars = input.toCharArray();
		if (chars[0] >= 'A' && chars[0] <= 'Z')
			return (char) (chars[0] - ('A' - 'a')) + input.substring(1);
		else {
			return input;
		}
	}

	/**
	 * 将enumeration里面的string首字母转换为大写的
	 * @param strs
	 * @return
	 * @throws NullPointerException
	 */
	public static ArrayList<String> firstChar2Upcase(Enumeration<String> strs,
			ArrayList<String> oldList) throws NullPointerException
	{
		if (strs == null)
			throw new NullPointerException();
		ArrayList<String> list = new ArrayList<String>();
		while (strs.hasMoreElements()) {
			String string = strs.nextElement();
			oldList.add(string);
			list.add(firstChar2Upcase(string));

		}
		return list;
	}

	/**
	 * 将null值转换为空字符串
	 * @param input
	 * @return
	 */
	public static String toNotNullString(String input)
	{
		return input == null ? "" : input.trim();
	}

	/**
	 * 转换为非null字符串
	 * @param input
	 * @param defaultString
	 * @return
	 */
	public static String toNotNullString(String input, String defaultString)
	{
		return input == null ? defaultString : input.trim();
	}

	/**
	 * 将空字符串和null转换为默认值
	 * @param input
	 * @param defaultString
	 * @return
	 */
	public static String toNotNullAndSpaceString(String input,
			String defaultString)
	{
		return input == null ? defaultString : input.equals("") ? defaultString
				: input.trim();
	}

	/**
	 * 将空字符串和null值转换为-1
	 * @param input
	 * @return
	 */
	public static String toNotNullNumberString(String input,
			String defaultString)
	{
		return input == null ? defaultString : input == "" ? defaultString
				: input.trim();
	}

	/**
	 * 如果input为NUll或为空就返回defaultString，否则返回input
	 * @param input
	 * @param defaultString
	 * @return
	 */
	public static String toDefault(String input, String defaultString)
	{
		if (input == null || input.equals(""))
			return defaultString;
		return input.trim();
	}

	/**
	 * 对url编码的字符串解码,url编码的原编码为UTF-8
	 * @param input
	 * @return
	 */
	public static String decodeURL(String input)
	{
		if (input == null)
			return null;
		try {
			return URLDecoder.decode(input, "UTF-8").trim();
		} catch (UnsupportedEncodingException e) {

			e.printStackTrace();
		}
		return input.trim();
	}

	public static String encodeUrl(String needEncode) {
		if (needEncode == null)
			return null;
		try {
			return URLEncoder.encode(needEncode, "UTF-8").trim();
		} catch (UnsupportedEncodingException e) {

			e.printStackTrace();
		}
		return needEncode.trim();
	}

	public static String decodeIphone(String input)
	{
		if (input == null)
			return null;
		return com.neusoft.www.framework.uitl.StringUtil.RunningDecode(input)
				.trim();

	}

	public static String buildStringIds(String checkIds) {
		if (checkIds == null)
			return null;
		String[] idString = checkIds.split(",");
		List<String> trueIDsStrings = new ArrayList<String>(idString.length);
		for (String id : idString) {
			if (InputCheck.isInt(id))
				trueIDsStrings.add(id);
		}
		if (trueIDsStrings.size() == 0)
			return null;
		StringBuilder ids = new StringBuilder();
		for (String id : trueIDsStrings) {
			ids.append(",").append(id);
		}
		ids.delete(0, 1);
		return ids.toString();
	}

	public static int[] buildIntIds(String checkIds) {
		if (checkIds == null || checkIds.equals(""))
			return null;
		String[] idString = checkIds.split(",");
		List<String> trueIDsStrings = new ArrayList<String>(idString.length);
		for (String id : idString) {
			if (InputCheck.isInt(id))
				trueIDsStrings.add(id);
		}
		if (trueIDsStrings.size() == 0)
			return null;
		int[] returnIds = new int[trueIDsStrings.size()];
		int i = 0;
		for (String stringId : trueIDsStrings) {
			returnIds[i] = Integer.parseInt(stringId);
			i++;
		}
		return returnIds;
	}

	// 创建唯一的协会的Id
	public static long[] buildLongIds(String checkIds) {
		if (checkIds == null || checkIds.equals(""))
			return null;
		String[] idString = checkIds.split(",");
		List<String> trueIDsStrings = new ArrayList<String>(idString.length);
		for (String id : idString) {
			if (InputCheck.isInt(id))
				trueIDsStrings.add(id);
		}
		if (trueIDsStrings.size() == 0)
			return null;
		long[] returnIds = new long[trueIDsStrings.size()];
		int i = 0;
		for (String stringId : trueIDsStrings) {
			returnIds[i] = Integer.parseInt(stringId);
			i++;
		}
		return returnIds;
	}

	public static String getNowTime()
	{
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	public static void main(String args[]) {
		System.out.println(ActionUtil.getAppendDateString(0).substring(0, 8)
				+ "000000");

	}
}
